package com.fan.analysis.module.preprocessing.message.service;

import com.fan.analysis.common.domain.ResponseDTO;
import com.fan.analysis.module.preprocessing.message.dao.MessageDao;
import com.fan.analysis.module.preprocessing.message.domain.vo.MessageInCompleteVo;
import com.fan.analysis.module.preprocessing.message.domain.vo.SpanMessageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 短信记录服务类
 *
 * @author fan
 * @date 2021/09/20 12:56
 **/
@Service
@Slf4j
public class MessageService {

    @Autowired
    private MessageDao messageDao;


    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<List<MessageInCompleteVo>> completingMessageData(Integer number) {

        // -------------- 1、获取20条未处理补齐的数据（flag=0），循环遍历进行处理 ------------------------
        List<MessageInCompleteVo> messageInCompleteVos = messageDao.listByNum(number);

        for (MessageInCompleteVo messageInCompleteVo : messageInCompleteVos) {

            // -------------- 2、补齐发送方与接收方 -------------------------------------
            String sendNum = messageInCompleteVo.getSendNum();
            String recNum = messageInCompleteVo.getRecNum();

            if ("".equals(sendNum) || "".equals(recNum)) {
                // 发送方与接收方有一个是空值，直接过
                continue;
            }

//            if ("".equals(sendNum)) {// 发送方为空，接收方非空
//
//                // 查询同一时间，发送方为该条记录的接收方的通话记录，如果有，则进行填充
//                MessageInCompleteVo byRecNum = messageDao.getByRecNum(recNum, messageInCompleteVo.getTime());
//                if (null != byRecNum) {
//                    messageDao.updateSendNum(byRecNum.getRecNum(), messageInCompleteVo.getId());
//                    messageInCompleteVo.setSendNum(byRecNum.getRecNum());
//                    messageInCompleteVo.getCompleting_fields().add("sendNum");
//
//                    // 短信内容有值的话，进行覆盖
//                    if (!"".equals(byRecNum.getContent()) && "".equals(messageInCompleteVo.getContent())) {
//
//                        messageDao.updateContent(byRecNum.getContent(), messageInCompleteVo.getId());
//                        messageInCompleteVo.setContent(byRecNum.getContent());
//                        messageInCompleteVo.getCompleting_fields().add("content");
//
//                    }
//
//                } else {
//                    continue;
//                }
//
//
//            } else if ("".equals(recNum)) {// 接收方为空，发送方非空
//                // 查询同一时间，接收方为该条记录的发送方的通话记录，如果有，则进行填充
//                MessageInCompleteVo bySendNum = messageDao.getBySendNum(sendNum, messageInCompleteVo.getTime());
//                if (null != bySendNum) {
//                    messageDao.updateRecNum(bySendNum.getSendNum(), messageInCompleteVo.getId());
//                    messageInCompleteVo.setRecNum(bySendNum.getSendNum());
//                    messageInCompleteVo.getCompleting_fields().add("recNum");
//
//                    // 短信内容有值的话，进行覆盖
//                    if (!"".equals(bySendNum.getContent()) && "".equals(messageInCompleteVo.getContent())) {
//
//                        messageDao.updateContent(bySendNum.getContent(), messageInCompleteVo.getId());
//                        messageInCompleteVo.setContent(bySendNum.getContent());
//                        messageInCompleteVo.getCompleting_fields().add("content");
//
//                    }
//                } else {
//                    continue;
//                }
//            }

            // 执行到这里，说明发送方与接收方都不为空
            // -------------- 3、补齐时间 ---------------------------------------

            if (null == messageInCompleteVo.getTime()) {
                // 查询两者之前的短信记录，最近的短信记录的开始时间 + 30分钟
                MessageInCompleteVo maxTime = messageDao.getMaxTime(messageInCompleteVo.getSendNum(), messageInCompleteVo.getRecNum());
                if (null != maxTime) {
                    Date s_time = maxTime.getTime();
                    long time = 30 * 60 * 1000;//30分钟
                    Date afterDate = new Date(s_time.getTime() + time);//30分钟后的时间
                    messageDao.updateTime(afterDate, messageInCompleteVo.getId());
                    messageInCompleteVo.setTime(afterDate);
                    messageInCompleteVo.getCompleting_fields().add("time");
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date now = new Date();
                    log.info("当前时间：" + sdf.format(now));
                    messageDao.updateTime(now, messageInCompleteVo.getId());
                    messageInCompleteVo.setTime(now);
                    messageInCompleteVo.getCompleting_fields().add("time");
                }
            }

            // -------------- 4、补齐短信内容 ---------------------------------------

            // -------------- 5、补齐地理位置 ---------------------------------------

            if ("".equals(messageInCompleteVo.getUserLocation())) {

                // 获取位置的众数
                MessageInCompleteVo modeLocation = messageDao.getModeLocation(messageInCompleteVo.getSendNum(), messageInCompleteVo.getRecNum());
                if (null != modeLocation) {

                    String userLocation = modeLocation.getUserLocation();
                    messageDao.updateUserLocation(userLocation, messageInCompleteVo.getId());
                    messageInCompleteVo.setUserLocation(userLocation);
                    messageInCompleteVo.getCompleting_fields().add("userLocation");
                }
            }

            // 经纬度保留2位小数
            String[] locationSplit = messageInCompleteVo.getUserLocation().split(",");

            for (int i = 0; i < locationSplit.length; i++) {
                float v = Float.parseFloat(locationSplit[i]);
                DecimalFormat df = new DecimalFormat("#0.00");
                String result = df.format(v);
                locationSplit[i] = result;
            }
            String locationNew = StringUtils.join(locationSplit, ",");
            messageInCompleteVo.setUserLocation(locationNew);

        }

        return ResponseDTO.succData(messageInCompleteVos);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<List<SpanMessageVo>> getSpamMessageData(Integer number) {

        List<SpanMessageVo> spanMessageVos = messageDao.listSpanMessageByNum(number);

        return ResponseDTO.succData(spanMessageVos);
    }
}
